package com.etc.controller;


import com.etc.pojo.SightDetail;
import com.etc.service.SightDetailService;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import java.io.File;
import java.io.IOException;

/**
 * @program: RookieSocialNetworking
 * @description: 动态的控制器类
 * @author: hz
 * @create: 2020-08-15 13:25
 **/
@Controller
@RequestMapping("/SightDetailController")
public class SightDetailController extends HttpServlet {
    Logger logger = Logger.getLogger(SightDetailController.class);


    @Autowired
    @Qualifier(value = "sightDetailImpl")
    SightDetailService service;


    /**
     * 后台修改景点信息
     */
    @RequestMapping(value="modifySightDetail",method =  RequestMethod.POST)
    @ResponseBody
    public  boolean modifySightDetail(@RequestParam(value = "sightId") int sightId,
                                      @RequestParam(value = "sightName") String sightName,
                                      @RequestParam(value = "sightAdd") String sightAdd,
                                      @RequestParam(value = "sightPrice") String sightPrice,
                                      @RequestParam(value = "sightOpenTime") String sightOpenTime,
                                      @RequestParam(value = "sightDescribe") String sightDescribe,
                                      @RequestParam(value = "sightImg") MultipartFile sightImg,
                                      @RequestParam(value = "sightStatus") String sightStatus) {
        String imgName = "无";
        logger.debug("第一次" + sightImg);
        if (sightImg != null) {
            //2.获取原始的文件名
            imgName = sightImg.getOriginalFilename();
            //3.定义文件的存储位置
            File file = new File("E:\\uploadFileImg\\" + imgName);
            //4.写入
            try {
                sightImg.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        logger.debug(imgName);
        boolean success = service.modifySightDetail(sightId, sightName, sightAdd, sightPrice, sightOpenTime, sightDescribe, imgName, sightStatus);
        return success;
    }

    /**
     * 后台删除景点信息
     */
    @RequestMapping("/delSightDetail")
    public int delSightDetail(int sightId) {
        return service.delSightDetail(sightId);
    }


    /**
     * //     * 模糊查询景点信息
     * //     *
     */
    @RequestMapping("/getAllSightDetail")
    @ResponseBody
    public PageInfo<SightDetail> getAllSightDetail(@RequestParam(value = "sightName", defaultValue = "") String sightName,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        PageInfo<SightDetail> pageInfo = service.selectSightDetailByName(sightName, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 后台添加景点
     */
    @RequestMapping("/addSightDetail")
    public String addSightDetail(@RequestParam(value = "sightName") String sightName,
                                 @RequestParam(value = "sightAdd") String sightAdd,
                                 @RequestParam(value = "sightPrice") String sightPrice,
                                 @RequestParam(value = "sightOpenTime") String sightOpenTime,
                                 @RequestParam(value = "sightDescribe") String sightDescribe,
                                 @RequestParam(value = "sightImg") MultipartFile sightImg,
                                 @RequestParam(value = "sightStatus") String sightStatus) {
        String imgName = "无";
        logger.debug("第一次" + sightImg);
        if (sightImg != null) {
            //2.获取原始的文件名
            imgName = sightImg.getOriginalFilename();
            //3.定义文件的存储位置
            File file = new File("E:\\uploadFileImg\\" + imgName);
            //4.写入
            try {
                sightImg.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logger.debug(imgName);
        boolean i = service.addSightDetail(sightName, sightAdd, sightPrice, sightOpenTime, sightDescribe, imgName, sightStatus);
        return "redirect:/showSightDetail.jsp";
    }
}
